package net.dgg.rz.production.queue;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Envelope;
import net.dgg.framework.tac.mq.common.repeated.DggMqRepeatedEnum;
import net.dgg.framework.tac.mq.rabbitmq.queue.normal.DggAbstractNormalConsumer;
import net.dgg.rz.common.constant.SystemCommonConstant;
import net.dgg.rz.production.service.OrderJsonService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * <p>
 *     消费签单人修改
 * </p>
 *
 * @author zyou
 * @date 2020/3/9 10:50
 */
@Component
public class UpdateSignUserConsumer extends DggAbstractNormalConsumer<Map> {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private OrderJsonService orderJsonService;

    @Override
    protected void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, Map data) {
        logger.info("收到订单中心更新签单人广播消息----------------》"+JSONObject.toJSONString(data));
        if(null == data){
            return;
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(data));
            Long orderId = jsonObject.getLong("orderId");
            Long signUserId = jsonObject.getLong("signUserId");
            String signUserName = jsonObject.getString("signUserName");
            if(null == orderId || signUserId==null|| StringUtils.isEmpty(signUserName)){
                return;
            }
            orderJsonService.executeUpdateSignUser(jsonObject);
        } catch (Exception e) {
            logger.error("签单商务修改消费失败", SystemCommonConstant.ORDER_CHANGE_SIGNUSER,SystemCommonConstant.UPDATE_SIGNUSER_NO_QUEUE,data);
        }
    }

    public UpdateSignUserConsumer() {
        this.queueName = SystemCommonConstant.UPDATE_SIGNUSER_NO_QUEUE;
        this.exchangeName = SystemCommonConstant.ORDER_CHANGE_SIGNUSER;
        this.hasDeadQueue = false;
        this.consumerRepeatedEnum = DggMqRepeatedEnum.DGG_MQ_REPEATED_NO;
    }
}
